package where2go.sctu.com.zhiyou.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.List;

import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.adpater.AppraiseAdapter;
import where2go.sctu.com.zhiyou.db.ScrollLinearLayoutManager;
import where2go.sctu.com.zhiyou.entity.Appraise;

/**
 * ProjectName: zhiyou
 * PackageName: where2go.sctu.com.zhiyou.fragment
 * Created by 张某人
 * CreatedTime:on 2018/5/8   00:35
 * Description: 线路评价
 */

public class LineAppraiseFragment extends Fragment{

    private RecyclerView mRecyclerView;
    private AppraiseAdapter mAdapter;
    private TextView mTextView;
    private List<Appraise.DataBean> dataList=new ArrayList<Appraise.DataBean>();

    private String classId="";
    private String url="http://47.104.80.155/youyiyou/comments/show?commentClassId=";
    private static LineAppraiseFragment fa;
    public static LineAppraiseFragment getLineAppraiseFragment() {
        if (fa == null) {
            fa = new LineAppraiseFragment();
        }
        return fa;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.line_appraise_fragment,null);
        findView(view);
        classId=getArguments().getString("id");
        getAppraiseDetail(url+classId+"&page=1");
        return view;
    }

    private void findView(View view) {
        mRecyclerView=(RecyclerView) view.findViewById(R.id.appraise_recyclerView);
        mTextView=(TextView) view.findViewById(R.id.appraise_course);

        mRecyclerView.setLayoutManager(new ScrollLinearLayoutManager(getContext(),
                ScrollLinearLayoutManager.VERTICAL,false));
    }

    private void getAppraiseDetail(String url){
        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String json) {  //onPostExecute
                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
                Appraise appraise= gson.fromJson(json, Appraise.class);
                dataList=appraise.getData();
                mAdapter=new AppraiseAdapter(getContext(),dataList);
                mRecyclerView.setAdapter(mAdapter);
                mAdapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("TAG", volleyError.toString());
                Toast.makeText(getActivity(), "网络请求失败，请检查网络", Toast.LENGTH_SHORT).show();
            }
//        第三部：把请求对象加入到请求的队列当中
        });
        request.setTag("getAppraise");
        MyApplication.getHttpQueues().add(request);
    }

    @Override
    public void onPause() {
        MyApplication.getHttpQueues().cancelAll("getAppraise");
        super.onPause();
    }

    @Override
    public void onDestroy() {
        MyApplication.getHttpQueues().cancelAll("getAppraise");
        super.onDestroy();
    }
}
